﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Com.Framework.Data;

namespace Com.Library.DB.Board
{
	public class ArticleModel
	{
		public enum BoardSNEnum : byte
		{
			Notice = 1,
			FAQ = 2,
			Request = 3,
			Contact = 4
		}

		private ArticleModel()
		{
		}

		public static int Create(byte BoardSN, string Title, string Content, string JsonData, int ManagerSNWriter, string UserID, string FileUrl)
		{
			ArticleCreate access = new ArticleCreate();

			ArticleCreateArguments args = new ArticleCreateArguments()
			{
				n1BoardSN = BoardSN,
				strTitle = Title,
				strContent = Content,
				strJSONData = JsonData,
				strFileUrl = FileUrl,
				n4ManagerSNWriter = ManagerSNWriter,
				strUserID = UserID
			};

			access.SetArguments(args);
			DataEntity output = access.ExecuteNonQuery();

			return output.GetData2("n4ArticleSN", 0);
		}

		public static int Create(byte BoardSN, string Title, string Content, string JsonData, int ManagerSNWriter, string UserID, string FileUrl, int ReadCount)
		{
			ArticleCreate access = new ArticleCreate();

			ArticleCreateArguments args = new ArticleCreateArguments()
			{
				n1BoardSN = BoardSN,
				strTitle = Title,
				strContent = Content,
				strJSONData = JsonData,
				strFileUrl = FileUrl,
				n4ManagerSNWriter = ManagerSNWriter,
				strUserID = UserID,
				n4ReadCount = ReadCount
			};

			access.SetArguments(args);
			DataEntity output = access.ExecuteNonQuery();

			return output.GetData2("n4ArticleSN", 0);
		}

		public static int Create(byte BoardSN, int CategoryNo, string Title, string Content, string JsonData, int ManagerSNWriter, string UserID, string FileUrl)
		{
			ArticleCreate access = new ArticleCreate();

			ArticleCreateArguments args = new ArticleCreateArguments()
			{
				n1BoardSN = BoardSN,
				n4CategoryNo = CategoryNo,
				strTitle = Title,
				strContent = Content,
				strJSONData = JsonData,
				strFileUrl = FileUrl,
				n4ManagerSNWriter = ManagerSNWriter,
				strUserID = UserID
			};

			access.SetArguments(args);
			DataEntity output = access.ExecuteNonQuery();

			return output.GetData2("n4ArticleSN", 0);
		}

		public static List<ArticleEntity> GetList(byte BoardSN, string Title_search, int PageNo, byte PageSize, out int CntTotal)
		{
			ArticleGetList access = new ArticleGetList();
			
			ArticleGetListArguments args = new ArticleGetListArguments()
			{
				n1BoardSN = BoardSN,
				strTitle_search = Title_search,
				PageNo = PageNo,
				PageSize = PageSize
			};

			access.SetArguments( args);

			List<ArticleEntity> articles = access.Execute();
			DataEntity output = access.GetOutput();

			CntTotal = output.GetData2("CntTotal", 0);

			return articles;
		}

		public static List<ArticleEntity> GetList(byte BoardSN, string Title_search, int CategoryNo, int PageNo, byte PageSize, out int CntTotal)
		{
			ArticleGetList access = new ArticleGetList();

			ArticleGetListArguments args = new ArticleGetListArguments()
			{
				n1BoardSN = BoardSN,
				strTitle_search = Title_search,
				n4CategoryNo = CategoryNo,
				PageNo = PageNo,
				PageSize = PageSize
			};

			access.SetArguments(args);

			List<ArticleEntity> articles = access.Execute();
			DataEntity output = access.GetOutput();

			CntTotal = output.GetData2("CntTotal", 0);

			return articles;
		}

		public static ArticleEntity GetInfo(int ArticleSN, bool RequireAdjacentInfo, string Title_Search)
		{
			return GetInfo(ArticleSN, RequireAdjacentInfo, Title_Search, true);
		}

		public static ArticleEntity GetInfo(int ArticleSN, bool RequireAdjacentInfo, string Title_Search, bool UpdateReadCount)
		{
			ArticleGetInfo access = new ArticleGetInfo();

			ArticleGetInfoArguments args = new ArticleGetInfoArguments()
			{
				n4ArticleSN = ArticleSN,
				isRequireAdjacentInfo = RequireAdjacentInfo,
				strTitle_Search = Title_Search,
				isUpdateReadCount = UpdateReadCount
			};

			access.SetArguments(args);

			ArticleEntity article = access.ExecuteNonQuery();

			if (article.n1BoardSN > 0)
				article.n4ArticleSN = ArticleSN;
			return article;
		}

		public static void Modify(int ArticleSN, string Title, string Content, string JsonData, string FileUrl)
		{
			ArticleModify access = new ArticleModify();
			ArticleModifyArguments args = new ArticleModifyArguments()
			{
				n4ArticleSN	 = ArticleSN,
				strTitle = Title,
				strContent = Content,
				strJSONData = JsonData,
				strFileUrl = FileUrl
			};

			access.SetArguments(args);

			access.ExecuteNonQuery();
		}

		public static void Modify(int ArticleSN, string Title, string Content, string JsonData, string FileUrl, int ReadCount)
		{
			ArticleModify access = new ArticleModify();
			ArticleModifyArguments args = new ArticleModifyArguments()
			{
				n4ArticleSN = ArticleSN,
				strTitle = Title,
				strContent = Content,
				strJSONData = JsonData,
				strFileUrl = FileUrl,
				n4ReadCount = ReadCount
			};

			access.SetArguments(args);

			access.ExecuteNonQuery();
		}

		public static void Remove(int ArticleSN)
		{
			ArticleRemove access = new ArticleRemove();
			ArticleRemoveArguments args = new ArticleRemoveArguments()
			{
				n4ArticleSN = ArticleSN
			};

			access.SetArguments(args);
			access.ExecuteNonQuery();
		}
	}
}
